package xcmg.device.web.buyBack;

import xcmg.device.infra.StringUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import xcmg.device.dao.entity.company.CompanySupplierDO;
import xcmg.device.dao.mapper.CompanySupplierMapper;
import xcmg.device.infra.BasicController;
import xcmg.device.infra.ErrorCode;
import xcmg.device.service.buyBack.BuyBackService;
import xcmg.device.service.vo.FunctionResult;
import xcmg.device.service.vo.buyBack.BuyBackAddVO;
import xcmg.device.service.vo.buyBack.BuyBackVO;
import yb.ecp.fast.infra.annotation.FastMappingInfo;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.infra.infra.PageCommonVO;
import yb.ecp.fast.infra.infra.SearchCommonVO;

import javax.servlet.http.HttpServletRequest;
import java.io.InputStream;
import java.util.Arrays;

@RestController
@RequestMapping("/buyBack")
public class BuyBackController extends BasicController {

    @Autowired
    private BuyBackService buyBackService;

    @Autowired
    private CompanySupplierMapper companySupplierMapper;

    @RequestMapping(value = "/list",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public ActionResult list(@RequestBody SearchCommonVO<BuyBackVO> condition)throws Exception{
        BuyBackVO buyBackVO = condition.getFilters();
        if(buyBackVO.getSeller()){
            buyBackVO.setSupplierOrgId(getOrgId());
        }else {
            buyBackVO.setPurchaseOrgId(getOrgId());
        }
        buyBackVO.setCreateOrgId(getOrgId());
        condition.setFilters(buyBackVO);
        PageCommonVO pageCommonVO = buyBackService.list(condition);
        return actionValueResult(pageCommonVO.getPageInfo());
    }

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public ActionResult add(@RequestBody BuyBackAddVO buyBackAddVO)throws Exception{
        BuyBackVO buyBackVO = buyBackAddVO.getHeaderInfo();
        if(buyBackVO.getSeller()){
            buyBackVO.setSupplierOrgId(getOrgId());
        }else {
            buyBackVO.setPurchaseOrgId(getOrgId());
        }
        buyBackVO.setCreateId(getUserId());
        buyBackVO.setCreateOrgId(getOrgId());
        buyBackVO.setCreateName(getUserName());
        buyBackAddVO.setHeaderInfo(buyBackVO);
        try {
            return actionResult(buyBackService.add(buyBackAddVO));
        }catch (Exception e){
            return new ActionResult(ErrorCode.Failure.getCode(), e.getMessage());
        }
    }

    @RequestMapping(value = "/item",method = RequestMethod.GET)
    @FastMappingInfo(needLogin = true)
    public ActionResult item(String id){
        return actionValueResult(buyBackService.item(id));
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public ActionResult update(@RequestBody BuyBackAddVO buyBackAddVO)throws Exception{
        try {
            return actionResult(buyBackService.update(buyBackAddVO));
        }catch (Exception e){
            return new ActionResult(ErrorCode.Failure.getCode(), e.getMessage());
        }
    }

    @RequestMapping(value = "/remove",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public ActionResult remove(@RequestBody String[] ids)throws Exception{
        if(ids==null||ids.length==0){
            return actionResult(ErrorCode.IllegalArument);
        }
        try {
            return actionResult(buyBackService.delete(Arrays.asList(ids)));
        }catch (Exception e){
            return new ActionResult(ErrorCode.Failure.getCode(), e.getMessage());
        }
    }

    @RequestMapping(value = "/audit",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public ActionResult audit(@RequestBody BuyBackVO buyBackVO)throws Exception{
        try {
            return actionResult(buyBackService.audit(buyBackVO, getUserId()));
        }catch (Exception e){
            return new ActionResult(ErrorCode.Failure.getCode(), e.getMessage());
        }
    }

    @RequestMapping(value = "/invalid",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public ActionResult invalid(@RequestBody BuyBackVO buyBackVO)throws Exception{
        try {
            return actionResult(buyBackService.invalid(buyBackVO.getId()));
        }catch (Exception e){
            return new ActionResult(ErrorCode.Failure.getCode(), e.getMessage());
        }
    }

    @RequestMapping(value = "/export",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public void export( String[] ids)throws Exception {
        buyBackService.export(Arrays.asList(ids));
    }

    @RequestMapping(value = "/exportAll",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public void exportAll(BuyBackVO condition)throws Exception {
        if(condition.getSeller()){
            condition.setSupplierOrgId(getOrgId());
        }else {
            condition.setPurchaseOrgId(getOrgId());
        }
        buyBackService.exportAll(condition);
    }

    @ResponseBody
    @RequestMapping(value = "/import", method = RequestMethod.POST)
    @ApiOperation("零件批量导入")
    @FastMappingInfo(needLogin = true)
    public ActionResult importExcel(HttpServletRequest request) throws Exception{
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        MultipartFile file = multipartRequest.getFile(multipartRequest.getFileNames().next());
        String fileName = file.getOriginalFilename();
        FunctionResult<String> result=new FunctionResult<>();
        String orgId = request.getParameter("x-orgId");
        if(StringUtil.isEmpty(orgId)){
            orgId = getOrgId();
        }
        if(StringUtil.isNotEmpty(orgId) && !orgId.equals(getOrgId())){
            CompanySupplierDO supplierDO = companySupplierMapper.itemBySupplierOrgId(getOrgId(), orgId);
            if(supplierDO == null || !supplierDO.getIsPlatCompany()){
                orgId = getOrgId();
            }
        }
        if (!fileName.trim().endsWith(".xls")) {
            result.setCode(ErrorCode.ImportExcelEndWithXLS);
        } else {
            InputStream in = file.getInputStream();
            result= buyBackService.importExcel(in, orgId);
        }
        return actionResult(result.getCode(), result.getValue());
    }

    @RequestMapping(value = "/download",method = RequestMethod.POST)
    @ApiOperation("下载零件批量导入模板")
    @FastMappingInfo(needLogin = true)
    public void download() throws Exception {
        buyBackService.download();
    }
}
